﻿<script>
    common.setMainTitle('User Management' + '<div>&nbsp;</div>');
</script>

<form id="mainform" name='frm' method=post action="">
    
	<div>
		<div id="toolbar-button">
			<div class="item" onclick="add()" name="addButton" id="addButton" title="Add">
				<div id="addnew-global-button"></div>
			</div>
			<div class="item" onclick="deleteAll()" name="addButton" id="deleteButton" title="Delete">
				<div id="delete-global-button"></div>
			</div>
			<div class="item" onclick="changeStatusMultiItems(1)" name="activateButton" id="activateButton" title="Activate">
				<div id="save-global-button"></div>
			</div>
			<div class="item" onclick="changeStatusMultiItems(0)" name="deactivateButton" id="deactivateButton" title="Deactivate">
				<div id="back-global-button"></div>				
			</div>
		</div>
	</div>
	
	<div>
		<table class="user-search" >	
        <tr>
            <td >
					<strong>Name</strong>
					<input type=text style="width:30px height:250px" id="searchString" name="searchString" value="<?=$this->name?>">
            </td>
			<td nowrap="nowrap">
					<strong>Email</strong>
					<input type=text id="searchEmail" name="searchEmail" value="<?=$this->email?>" >
            </td>
			<td nowrap="nowrap">
					<strong>Role</strong>					
					<select name ="searchRole" id="searchRole" style="width: 205px" value="<?=$this->roles?>" >
						<option value="">-- Select a role --</option>
						<?
						$selected = '';
						foreach($this->listRole as $role){
							if($role['ROLEID'] == $this->roles ){
								$selected = 'selected';
							}else{
								$selected = '';
							}
						?>
						<option <?=$selected?> value="<?=$role['ROLEID']?>"><?=$role['NAME']?></option>
						<? } ?>
					</select>
            </td>
			<td nowrap="nowrap" >
					<strong>Status</strong>
					<select type=text id="searchStatus"  name="searchStatus" value="<?=$this->status?>">	
						<option style="margin: 5px" value=""  <?= ($this->status=="")? 'selected':"" ?>  >All</option>
						<option value="1" <?= ($this->status=="1")? 'selected':"" ?> >Actived</option>
						<option value="0" <?= ($this->status=="0")? 'selected':"" ?> >Inactived</option>
					</select>
            </td>
            <td>
				<input type='submit' onClick='validate();' value="Search">
            </td>
        </tr>
    </table >
	</div>
	
    <table style="margin-top: 10px;clear: both" border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
        <thead>
            <tr class="alternate-row">
                <th class="table-header-repeat line-left" width="3%" style="text-align: center" nowrap >
                    <input type=checkbox name=DELALL onclick="SelectAll(this,'ITEM')">
                </th>
                <th class="table-header-repeat line-left" width="20%" ><a>FIRSTNAME</a></th>
				<th class="table-header-repeat line-left" width="22%" ><a>LASTNAME</a></th>
				<th class="table-header-repeat line-left" width="27%" ><a>EMAIL</a></th>
				<th class="table-header-repeat line-left" width="8%" ><a>ROLE</a></th>
				<th class="table-header-repeat line-left" width="5%" ><a>STATUS</a></th>
				<th class="table-header-repeat line-left" width="" ><a>ACTION</a></th>
                
            </tr>
        </thead>
        <tfoot>
        </tfoot>
        <tbody>
            <?php
            if(count($this->data)==0){
				echo "<tr><td colspan=7>There is not any data</td></tr>";
            } else {
                $stt = 1;
            }
            foreach($this->data as $item){?>
            <tr>
                <td style="text-align: center;padding-left: 0">
                    <input id="<?=$item['USERID']?>" type="checkbox" name="ITEM[]" value="<?=$item['USERID']?>">
                </td>
                <td> 
					<div>
                        <?=$item['FIRSTNAME'];?>
					</div>
                </td>
				<td>
					<div>
                        <?=$item['LASTNAME'];?>
					</div>
                </td>
                <td>
					<div>
                        <?=$item['EMAIL'];?>
					</div>
                </td>
				<td>
					<div>
						<?=$item['NAME'];?>
					</div>
                </td>
                <td style="text-align: center">
					<span id="status<?=$item['USERID']?>">
                        <? if($item['STATUS']!='1') { ?>
                            <a href="#" onClick="changeStatus(<?=$item['USERID'] ?>,1)">Inactive</a>
                        <?} else {?>
                            <a href="#" onClick="changeStatus(<?=$item['USERID'] ?>,0)">Active</a>
                        <?}?>
                    </span>
                </td>
				<td style="text-align: center"> 
					<div>
					<select id="<?php echo 'actionbox'.$item['USERID'] ?>" name="<?php echo 'actionbox'.$item['USERID'] ?>" onchange="chooseAction(<?=$item['USERID'] ?>)">
						<option value="0" >Choose an Action</option>
						<option value="<?=$item['USERID'] ?>" >Edit</option>
						<option value="<?=$item['USERID'] ?>" >Delete</option>
						<option value="<?=$item['USERID'] ?>" >Change Password</option>
					</select>
					</div>
                </td>
            </tr>
            <? $stt++;}?>
        </tbody>
    </table>
	
	<div id="pagination-div" width="100%">
		<?= $this->pagination?>
	</div>
</form>
<script type="text/javascript"> 
    function chooseAction(userId){
		text = $("#actionbox"+userId+ " option:selected").text();
		if(text == 'Edit'){
			window.location = "/admin/users/input/id/"+userId + "/limit/" + '<?= $this->limit?>/page/' + '<?= $this->page?>';
		}else if(text == "Delete"){
			check = confirm("Do you want to delete this user?");
			if(check){
				window.location = "/admin/users/delete/id/" +userId + "/limit/" + '<?= $this->limit?>/page/' + '<?= $this->page?>';
			}
		}else if(text == "Change Password"){
			window.location = "/admin/users/changepass/id/" +userId + "/limit/" + '<?= $this->limit?>/page/' + '<?= $this->page?>';
		}
    }
    
    function add(){
        window.location ='/admin/users/input';
    }
	
    function SelectAll(selobj,childobj){
         var arr = document.getElementsByName(childobj+"[]");
         for(var i=0;i<arr.length;i++){
            arr[i].checked = selobj.checked;
         }
    }
	
     /* update active (ajax) */
    //Gui thong tin status len server
    function changeStatus(itemID, status){
        $.ajax({
			url: "/admin/users/changestatus",
			type: "POST",
			data: {itemID : itemID, status: status},
			dataType: "text",
			success: function(data) {
				if(status == 0){
					$('#status' + itemID).html('<a href="#" onClick="changeStatus('+ itemID+ ',1)">Inactive</a>');
				} else {
					$('#status' + itemID).html('<a href="#" onClick="changeStatus('+ itemID+ ',0)">Active</a>');
				}
			}
		});
    }
	
	function changeStatusMultiItems(status){
		var ln = 0;
		var arr = document.getElementsByName('ITEM[]');
		var arrValue = new Array();
			
		for(var i = 0 ; i < arr.length ;i++ )
		{
				if(arr[i].checked == true)
				{
						arrValue[i] = arr[i].value;
				}
		}
		for(var i = 0 ; i < arr.length ;i++ )
		{

				if(arr[i].checked == true)
				{
						ln = 1;
						break;
				}
		}
		if(ln == 1)
		{
		    var question;
		    if(status ==1){
			    question = 'Do you want to activate these items?';
		    } else {
			    question = 'Do you want to inactivate these items?';
		    }
		    
		    if(confirm(question)){
			$.ajax({
				url: "/admin/users/changestatusmultiitems",
				type: "POST",
				data: {itemIDs : arrValue, status: status},
				dataType: "text",
				success: function(data) {
					for(var i = 0 ; i < arr.length ;i++ )
					{
						if(status == 0){
							$('#status' + arrValue[i]).html('<a href="#" onClick="changeStatus('+ arrValue[i]+ ',1)">Inactive</a>');
						} else {
							$('#status' + arrValue[i]).html('<a href="#" onClick="changeStatus('+ arrValue[i]+ ',0)">Active</a>');
						}
						arr[i].checked=false;
					}
				}
			});
		    }
		}
		else
		{
		    alert("You must choose at least 1 item!");
		}
	}
	
    function deleteAll()
    {
		var ln = 0;
		var arr = document.getElementsByName('ITEM[]');
		for(var i = 0 ; i < arr.length ;i++ )
		{

			if(arr[i].checked == true)
			{
			ln = 1;
			break;
			}
		}
		if(ln == 1)
		{
			if(confirm("<?php echo "Do you want to delete these items?"; ?>")){
			document.frm.action="/admin/users/deletemultiitem/limit/" + '<?= $this->limit?>/page/' + '<?= $this->page?>';
			document.frm.method="post";
			document.frm.submit();
			}
		}
		else
		{
			alert("You must choose at least 1 item!");
		}
    }
	
	function checkEmail(email){
		//alert("Email is invalid.!");
		if(preg_match("/[a-zA-Z0-9_-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", email) > 0)
		{		
			alert("OK!");
			//return true;
		}
		else
		{
			alert("Email is invalid!");
			//return false;
		}
	}
	
	function validate()
	{
		//var tmpEmail = document.getElementById("searchEmail");
		//alert(tmpEmail.value);
		//checkEmail(tmpEmail.value);
		//var tmpRole = document.getElementById("searchRole");			
		//alert(tmpRole.value);
	}
		
</script>
